#!/usr/bin/env bash

# sh -x demo.sh
# test shell

# protoc
function get_protoc() {

    # 下载安装包 (Mac)
    wget https://github.com/protocolbuffers/protobuf/releases/download/v3.15.6/protoc-3.15.6-osx-x86_64.zip
    # 解压到 /usr/local 目录下
    unzip protoc-3.15.6-osx-x86_64.zip -d protoc-3.15.6-osx-x86_64
    mv protoc-3.5.0-osx-x86_64/bin/protoc /usr/local/bin/protoc
    # 执行如下表示成功：
    echo $(protoc --version)
    # should echo such as "libprotoc 3.15.6"
}

# protoc
function go_demo_golang() {
    # support protoc --go_out=. test.proto
    go get -u github.com/golang/protobuf/protoc-gen-go

}

function gogo_demo_gofast() {
    # support protoc --gofast_out=. myproto.proto
    go get github.com/gogo/protobuf/protoc-gen-gofast

}

function get_protoc_gogo_engine() {

    if [ "$1"x != "x" ]; then
        go get github.com/gogo/protobuf/$1
    else
        echo "null input"
    fi
}

function get_protogogo_etc() {
    go get github.com/gogo/protobuf/proto
    get_protoc_gogo_engine protoc-gen-$1
    go get github.com/gogo/protobuf/gogoproto
    # protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --$1_out=. myproto.proto
    #// 这里的{binary}不包含「protoc-gen」前缀
}

function get_protoc_gogo_slick() {
    get_protogogo_etc gogoslick
}

function get_protoc_gogo_faster() {
    get_protogogo_etc gogofaster
}

function get_protoc_gogo_faster() {
    get_protogogo_etc gogofast
}

function get_gogo_other() {
    go get github.com/gogo/protobuf/proto
    go get github.com/gogo/protobuf/jsonpb
    go get github.com/gogo/protobuf/protoc-gen-gogo
    go get github.com/gogo/protobuf/gogoproto
}

function generate_simple_protofile() {
    ## cat > proto write to file
    cat <<EOF >my.proto
syntax = "proto3";                // proto 版本，建议使用 proto3
option go_package = "main/proto"; // 包名声明符

message SearchRequestParam {      // message 类型
  enum Type {                     // 枚举类型
    PC = 0;
    Mobile = 1;
  }
  string query_text = 1;          // 字符串类型 | 后面的「1」为数字标识符，在消息定义中需要唯一
  int32 limit = 3;                // 整型
  Type type = 4;                  // 枚举类型
}

message SearchResultPage {
  repeated string result = 1;     // 「repeated」表示字段可以重复任意多次（包括0次）
  int32 num_results = 2;
}
// test.proto
EOF
}

function echo_something() {
    echo "$1"
}

function noparaval() {
    echo "opt: [$1] no paraval [$2]"
    echo ""
    help
}

function invalid() {
    echo "无效的选项 - $1"
    echo ""
    help
}


function help() {
    cat <<EOF
OVERVIEW: cup shell for compile executor project

USAGE: ./cup.sh [options] <inputs>

OPTIONS:
    -h help
COMMOM USAGE:
    -g ggg

EOF
}

function main() {
    while getopts ":R:g" opt; do
        # printOpt $opt $OPTARG
        case $opt in
        R) echo_something $OPTARG ;;
        a) all ;;
        # c) clean ;;
        # C) force_clean ;;
        g) generate_simple_protofile ;;
        h) help ;;
        :)
            noparaval $opt $OPTARG
            exit 1
            ;;
        ?)
            invalid $OPTARG
            exit 2
            ;;
        esac
    done
}

main "$@"
